package com.wuyong.Chapter3;

import com.wuyong.MyArrayList;

/**
 * 数组队列
 * 缺点
 * 取元素 时间复杂度 O(n)
 * @param <E>
 */
public class ArrayQueue<E> implements Queue<E> {

    private MyArrayList array;
    public ArrayQueue() {
        array = new MyArrayList();

    }
    @Override
    public void enqueue(E e) {
        array.addLast(e);
    }

    @Override
    public E dequeue() {
        return (E) array.removeFirst();
    }

    @Override
    public E getFront() {
        return (E) array.getFirst();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Queue: ");
        sb.append("head [");
        for (int i = 0; i < array.getSize(); i++) {
            sb.append(array.get(i));
            if (i != array.getSize() -1) {
                sb.append(", ");
            }
        }
        sb.append("] tail");
        return sb.toString();
    }

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue();
        for (int i = 0; i < 20; i++) {
            arrayQueue.enqueue(i);
            System.out.println(arrayQueue);
            if (i % 3 == 2) {
                arrayQueue.dequeue();
                System.out.println(arrayQueue);
            }
        }

    }
}
